
var SeopHighlighter = new function() {
  this.style = null;
  this.hl_list = [];

  this.link_comparator = {
    all: function()  {  return true;        },
    none: function() { return false; },
    nofollow: function(domain, x)  {   return -1!=x.rel.toLowerCase().indexOf("nofollow"); },
    internal: function(domain, x)   {  return SeoprofesionalTools.get_domain(x.href)==domain;    },
    external: function(domain, x)   {  return !SeopHighlighter.link_comparator.internal(domain, x);  },
    _x_sentinel_0: 0
  };

  return this;
};

chrome.extension.onConnect.addListener(function(port) {
  if ("seophl"!=port.name) {
    return;
  }
  port.onMessage.addListener(function(param) {
    if ("focus"==param.action) {
      if (param.frame!=document.documentURI) {
        return ;
      }
      var el = document.links[param.index];
      el.focus();
      var range = document.createRange(el);
      range.selectNode(el);
      var sel = window.getSelection();
      sel.removeAllRanges();
      sel.addRange(range);
    } else if ("select"==param.action) {
      var domain = SeoprofesionalTools.get_domain(document.documentURI);
      var do_hl = function() { return false; };
      if (param.type in SeopHighlighter.link_comparator) {
        do_hl = SeopHighlighter.link_comparator[param.type];
      }
      var msg = [];
      for (var i = 0;i<document.links.length;i++) {
        var el = document.links[i];
        if (do_hl(domain, el)) {
          el.className+= " __seop_hl1__";
          msg.push({"name":el.href, "frame": document.documentURI, index: i, "href":el.href});
        } else {
          el.className = el.className.replace("__seop_hl1__", "");
        }
      }
      port.postMessage(msg);
    }
  });
});

